import { RouteRecordRaw } from 'vue-router';
import store from '../store';

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Home',
        meta: { requiresAuth: true },
        component: () => import('../views/Home.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/protected',
        name: 'Protected',
        component: () => import('../views/Protected.vue'),
        meta: { requiresAuth: true }
    }
];

// 路由守卫
export function setupRouterGuard(router: any) {
    router.beforeEach((to: any, from: any, next: any) => {
        if (!window.history.state.current) {
            window.history.state.current = to.path;
        }
        if (!window.history.state.back) {
            window.history.state.back = from.path;
        }
        console.log('app1路由',to.path)
        if (to.matched.some((record: any) => record.meta.requiresAuth)) {
            // // 判断是否需要登录权限
            // if (!store.state.token) {
            //     // 未登录则跳转到登录页面
            //     next({
            //         path: '/login',
            //         // 保存要跳转的路径，登录成功后跳转到该路径
            //         query: { redirect: to.fullPath }
            //     });
            // } else {
            //     next();
            // }

            next();
        } else {
            next();
        }
    });
}

export default routes;